package com.kaishengit.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.TaskType;
import com.kaishengit.pojo.User;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.TaskTypeService;
import com.opensymphony.xwork2.ActionSupport;

public class TaskAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private String taskname;
	private String selecttime;
	private String leibie;
	private String taskType;
	private TaskService taskService;
	private String settimeinput;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Task task ;
	private String id;
	private List<Task> list;
	private List<TaskType> list2;
	private TaskTypeService taskTypeService;
	
	public String execute(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		try {
			list = taskService.findAllByUser(user);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	public String totaskType(){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		list2 = taskTypeService.findTypeByUser(user);
		return SUCCESS;
	}
	public String editTask(){
		System.out.println(id);
		taskService.edit(id,taskname,selecttime,settimeinput,taskType);
		return SUCCESS;
		
	}
	
	public String addTask() throws ParseException, IOException{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("curr_user");
		/*Task task = taskService.save(taskname,selecttime,leibie,taskType,settimeinput,user);
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(task));
		out.flush();
		out.close();
		return null;*/
		System.out.println(id);
		task  = taskService.save(taskname,selecttime,leibie,taskType,settimeinput,user,id);
		task.setUser(null);
		//list = taskService.findTaskByType(taskType);
		return SUCCESS;
		/*if("leibie".equals("person")){
			taskService.save(taskname,selecttime,leibie,taskType,settimeinput,user);
			return SUCCESS;
		}else{
			Task task = taskService.save(taskname,selecttime,leibie,taskType,settimeinput,user);
			Gson gson = new Gson();
			PrintWriter out = response.getWriter();
			out.print(gson.toJson(task));
			out.flush();
			out.close();
			return null;
		}*/
		
	}
	public String deltask(){
		taskService.delById(id);
		return "success";
	}
	//set
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public String getTaskname() {
		return taskname;
	}
	public void setTaskname(String taskname) {
		this.taskname = taskname;
	}
	public String getSelecttime() {
		return selecttime;
	}
	public void setSelecttime(String selecttime) {
		this.selecttime = selecttime;
	}
	public String getLeibie() {
		return leibie;
	}
	public void setLeibie(String leibie) {
		this.leibie = leibie;
	}
	public String getTaskType() {
		return taskType;
	}
	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}
	public String getSettimeinput() {
		return settimeinput;
	}
	public void setSettimeinput(String settimeinput) {
		this.settimeinput = settimeinput;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<Task> getList() {
		return list;
	}
	public void setList(List<Task> list) {
		this.list = list;
	}
	public List<TaskType> getList2() {
		return list2;
	}
	public void setList2(List<TaskType> list2) {
		this.list2 = list2;
	}
	public TaskTypeService getTaskTypeService() {
		return taskTypeService;
	}
	public void setTaskTypeService(TaskTypeService taskTypeService) {
		this.taskTypeService = taskTypeService;
	}
	
	
	
	
	
	

}
